# encoding=utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from reward import getAllRewardsNames
from ui.ui_hd1_reward import Ui_rewardDlg

import copy

class hd1RewardDlg(QDialog, Ui_rewardDlg):

	def __init__(self, parent=None):
		super(hd1RewardDlg, self).__init__(parent)
		self.setupUi(self)
		self.m_Id = 1
		self.m_CfgInfo = {}
		self.m_CfgList = []
		self.init()
		self.initSignals()

	def init(self):
		self.rewardTbl.setColumnWidth(0, 20)  # 设置表格0列列宽
		self.rewardTbl.setColumnWidth(0, 80)  # 设置表格0列列宽
		self.rewardTbl.setColumnWidth(0, 70)  # 设置表格0列列宽

	def initSignals(self):
		self.addRewardBtn.clicked.connect(self.addReward)
		self.rewardTbl.clicked.connect(self.enableDelBtn)
		self.delRewardBtn.clicked.connect(self.delReward)
		self.resetBtn.clicked.connect(self.clearReward)

	def clear(self):
		self.m_Id = 1
		self.m_CfgInfo = {}
		self.m_CfgList = []

	def clearUi(self):
		self.rewardTbl.clear()
		self.rewardTbl.setRowCount(0)

	def addReward(self):
		iRow = self.rewardTbl.rowCount()
		self.rewardTbl.setRowCount(iRow + 1)
		idx = str(self.m_Id)

		rewardComboBox = QComboBox()
		for name in getAllRewardsNames():
			rewardComboBox.addItem(name)

		rewardCntBox = QSpinBox()
		rewardCntBox.setMaximum(50)
		rewardCntBox.setMinimum(1)
		item = QTableWidgetItem(idx)
		item.setFlags(item.flags() & Qt.ItemIsEnabled | Qt.ItemIsSelectable)
		self.rewardTbl.setItem(iRow, 0, item)
		self.rewardTbl.setCellWidget(iRow, 1, rewardComboBox)
		self.rewardTbl.setCellWidget(iRow, 2, rewardCntBox)

		self.m_Id += 1

	def enableDelBtn(self):
		self.delRewardBtn.setEnabled(True)

	def disableDelBtn(self):
		self.delRewardBtn.setEnabled(False)

	def delReward(self):
		iId = self.rewardTbl.item(self.rewardTbl.currentRow(), 0).text()
		title = '删除奖励'
		content = f'确定删除id为{iId}的奖励吗'
		question = QMessageBox.information(self, title, content, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
		if question == QMessageBox.Yes:
			self.rewardTbl.removeRow(self.rewardTbl.currentRow())
			if iId in self.m_CfgInfo:
				self.m_CfgInfo.pop(iId)
				QMessageBox.information(self, title, "删除成功", QMessageBox.Yes)

	def clearReward(self):
		self.m_Id = 1
		rewardTbl = self.rewardTbl
		rows = rewardTbl.rowCount()

		for row in range(rows-1, -1, -1):  # 删除首行后，后面的行索引会前移，所以需要倒序删除
			self.rewardTbl.removeRow(row)

	def closeEvent(self, event):
		self.saveReward()
		if not self.m_CfgList:
			return
		self.parent().m_rewardedList = copy.deepcopy(self.m_CfgList)
		super().closeEvent(event)

	# 保存tbl数据到字典，太长了，需优化下函数
	def saveReward(self):
		rewardTbl = self.rewardTbl
		rows = rewardTbl.rowCount()
		if not rows:
			return
		cols = rewardTbl.columnCount()
		for row in range(rows):
			lstRewardInfo = []
			for col in range(cols):
				if rewardTbl.item(row, col):
					lstRewardInfo.append(rewardTbl.item(row, col).text())
					print(rewardTbl.item(row, col).text())
				elif rewardTbl.cellWidget(row, col):
					widget = rewardTbl.cellWidget(row, col)
					if isinstance(widget, QComboBox):
						lstRewardInfo.append(widget.currentText())
					elif isinstance(widget, QSpinBox):
						lstRewardInfo.append(widget.value())
			self.m_CfgInfo[lstRewardInfo[0]] = [lstRewardInfo[1], lstRewardInfo[2]]
			self.m_CfgList.append([lstRewardInfo[1], lstRewardInfo[2]])
